package com.tind.wyf.baselib.adapter;

import android.annotation.SuppressLint;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 *
 * viewholder 适配器
 */
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> mViews;

    public RecyclerViewHolder(@NonNull View itemView) {
        super(itemView);
        mViews = new SparseArray<View>();
    }

    //通过viewId获取控件
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public TextView getTextView(@IdRes int id) {
        return getView(id);
    }

    public Button getButton(@IdRes int id) {
        return getView(id);
    }

    public void setTextView(@IdRes int id, String text) {
        TextView view = getView(id);
        view.setText(text);
    }

    @SuppressLint("ResourceType")
    public void setImageView(@IdRes int id, @DrawableRes int drawableId) {
        ImageView view = getView(id);
        view.setImageResource(drawableId);
    }
}
